package payment;

import com.example.utils.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.annotation.KafkaListener;
import payment.service.PaymentService;

import javax.annotation.Resource;

@SpringBootApplication
@EnableFeignClients
@EnableKafka
public class PaymentApplication {
    public static void main(String[] args) {
        SpringApplication.run(PaymentApplication.class, args);
    }

    private static final Logger LOG = LoggerFactory.getLogger(PaymentApplication.class);

    @Resource
    PaymentService paymentService;

    @KafkaListener(id = "orders", topics = "orders", groupId = "payment")
    public void onEvent(Message m) {
        LOG.info("Received: {}" , m);
        if (m.getStatus().equals("SIGNED"))
            paymentService.settle(m);
        else paymentService.rollback(m);
    }
}
